package tweeks.management.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import tweeks.category.domain.Category;
import tweeks.category.service.CategoryService;

@Controller
@RequestMapping(value="/management/category")
public class CategoryManagementController {
	
	@Resource(name="CategoryServiceImpl")
	private CategoryService categoryService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String view(Category category, ModelMap model) throws IOException {
		Category categoryTree = categoryService.generateCategoryTree(category);
		List<Category> categoryList = categoryService.generateCategoryList(categoryTree);
		
		model.addAttribute("category", category);
		model.addAttribute("categoryList", categoryList);
		model.addAttribute("categoryTree", categoryTree);
		
		return "management/category";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/create")
	public String create(@ModelAttribute Category category, ModelMap model) throws IOException {
		categoryService.createCategory(category);
		return "redirect:/management/category";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/delete")
	public String delete(@ModelAttribute Category category, ModelMap model) throws IOException {
		categoryService.deleteCategory(category);
		return "redirect:/management/category";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/modify")
	public String modify(@ModelAttribute Category category, ModelMap model) throws IOException {
		categoryService.modifyCategory(category);
		return "redirect:/management/category";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/upwardRelocate")
	public String upwardRelocate(@ModelAttribute Category category, ModelMap model) throws IOException {
		categoryService.upwardRelocate(category);
		return "redirect:/management/category";
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/downwardRelocate")
	public String downwardRelocate(@ModelAttribute Category category, ModelMap model) throws IOException {
		categoryService.downwardRelocate(category);
		return "redirect:/management/category";
	}
	
}
